Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1431 posts 3332 karma points c-trib
    May 04, 2009 @ 18:08
    Simon Dingley
    0

    Custom DataType(DropDownList) Problem [SOLVED]

    I am having problems with a custom data type and have wasted a lot of time now trying to get to the bottom of it and with no results so I could do with another pair of eyes.

    Initially the control loads fine with all data in place as expected, on Postback I lose the data and selected value, its probably just an oversight on my part but any help would be appreciated.

    My control inherits from a dropdownlist:

    [code] public class BranchDropDownList : System.Web.UI.WebControls.DropDownList
    {
    protected override void OnInit(EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
    //- Load the branches.config file
    xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/data/branches.config"));
    //- LINQ query to get values from branch config
    this.Items.AddRange((from System.Xml.XmlElement el in xmldoc.SelectNodes("/*/branch")
    select new ListItem(el.GetAttribute("name"), el.GetAttribute("id"))).ToArray());
    }

    base.OnInit(e);
    }
    }[/code]

    ...and my data type is as follows:

    [code] ///


    /// A Branch list data type
    ///

    public class BranchDataType : AbstractDataEditor
    {
    #region -- Fields --

    BranchDropDownList ddlBranchList = new BranchDropDownList();

    #endregion Fields 

    #region -- Constructors --

    public BranchDataType()
    {
    base.RenderControl = ddlBranchList;
    ddlBranchList.Init += new EventHandler(ddlBranchListInit);
    //- Subscribe to the DataEditorControl.OnSave event
    base.DataEditorControl.OnSave += new AbstractDataEditorControl.SaveEventHandler(BranchDataType
    OnSave);
    }

    #endregion Constructors 

    private void ddlBranchListInit(object sender, EventArgs e)
    {
    if (base.Data.Value != null)
    {
    //- Set the selected branch in the list if it exists
    if (this.ddlBranchList.Items.FindByValue(base.Data.Value.ToString()) != null)
    {
    this.ddlBranchList.Items.FindByValue(base.Data.Value.ToString()).Selected = true;
    }
    }
    }

    ///
    /// OnSave event handler for the BranchDataType
    ///

    /// The instance containing the event data.
    private void BranchDataType
    OnSave(EventArgs e)
    {
    base.Data.Value = ddlBranchList.SelectedValue;
    }

    #region -- Properties --

    ///
    /// Gets the name of the data type.
    ///

    ///
    public override string DataTypeName
    {
    get
    {
    return "Branch List";
    }
    }

    ///
    /// Gets the unique id for the datatype.
    ///

    ///
    public override Guid Id
    {
    get
    {
    return new Guid("50417222-2912-11DE-B5C6-6DAE56D89593"); ;
    }
    }

    #endregion Properties 
    }[/code]

  • Simon Dingley 1431 posts 3332 karma points c-trib
    May 05, 2009 @ 17:06
    Simon Dingley
    0

    Finally I appear to have a solution.
    Thanks to this post:
    http://geekswithblogs.net/mikethomas/archive/2007/01/15/103686.aspx

    I now have a control that loads data in the DataBind event so the class now looks as follows:
    [code] public class BranchDropDownList : System.Web.UI.WebControls.DropDownList
    {

    public override void DataBind()
    {
    this.Items.Clear();
    System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
    //- Load the branches.config file
    xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/data/branches.config"));
    //- LINQ query to get values from branch config
    this.Items.AddRange((from System.Xml.XmlElement el in xmldoc.SelectNodes("/*/branch")
    select new ListItem(el.GetAttribute("name"), el.GetAttribute("id"))).ToArray());
    base.DataBind();
    }

    protected override void EnsureDataBound()
    {
    try
    {
    if (this.RequiresDataBinding)
    {
    this.DataBind();
    }
    }
    catch { }
    }
    }[/code]

    And the selected value is set when the control has been DataBound so in my BranchDataType I simply subscribe to the BranchDropDownList controls DataBound event instead of the Init event.

    Jobs good'n...

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies